package com.lankystar.sos.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sound.midi.SysexMessage;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lankystar.sos.dao.IStatusDao;
import com.lankystar.sos.dao.IUserDao;
import com.lankystar.sos.dao.IUserLocationDao;
import com.lankystar.sos.model.AuthStatus;
import com.lankystar.sos.model.ResponseOrder;
import com.lankystar.sos.model.SosOrder;
import com.lankystar.sos.model.SosStatus;
import com.lankystar.sos.model.User;
import com.lankystar.sos.model.UserApprove;
import com.lankystar.sos.model.UserLocation;
import com.lankystar.sos.model.UserType;
import com.lankystar.sos.service.IUserInterface;

@Service
@Transactional
public class UserInterface implements IUserInterface{

	private static final SosStatus[] ob = null;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IUserLocationDao locationDao;
	@Autowired
	private IStatusDao authDao;
	@Override
	public Map<String, Object> userLoginMethod(String userPhone, String userPassword) {
		Map<String, Object> map = new HashMap<>();
		Object[] ob = userDao.selectUserByNameAndPass(userPhone, userPassword);
		User user = (User) ob[0];
		UserType userType = (UserType) ob[2];
		/**
		 * 注册时，生成认证表，用户类型为最基本的   求救者类型
		 */
		System.out.println("userPhone========>"+user.getUserPhone()+user.getUserEmail());
		map.put("userId", user.getUserId());
		map.put("userPhone", user.getUserPhone());
		map.put("userEmail", user.getUserEmail());
		map.put("userPortrait", user.getUserPortrait());
		map.put("userNickName", user.getUserNickName());
		map.put("userGender", user.getUserGender());
		map.put("userBirthday", user.getUserBirthday());
		map.put("userType", userType.getUserkey());
		return map;
	}
	@Override
	public void userRegisterMethod(User record) {
		userDao.addUser(record);
		User user = userDao.selectUserByuserPhone(record.getUserPhone());
		UserType userType = userDao.selectUserTypeByuserkey(0);
		UserApprove userApprove = new UserApprove();
		userApprove.setUser(user);
		userApprove.setUserType(userType);
		userDao.addUserApprove(userApprove);
	}
	@Override
	public Map<String, Object> userApproveMethod(File idCardBack,File idCardFront,String idCard, Integer userId,Integer userkey) {
		/**
		 * 用户认证是需要的接口
		 */
		Object[] ob = userDao.selectUserApproveAndUserByUserId(userId);
		UserApprove userApprove = (UserApprove) ob[1];
		UserType userType = userDao.selectUserTypeByuserkey(userkey);
		AuthStatus auth = authDao.selectAuthStatusByauthkey(0);//认证状态  -->认证中
		Map<String, Object> mapPath = new HashMap<>();
		try {
			String idCardBackPath = FileServiceImpl.uploadFile(idCardBack);
			String idCardFrontPath = FileServiceImpl.uploadFile(idCardFront);
			userApprove.setUserType(userType);
			userApprove.setAuthStatus(auth);
			userApprove.setIdCard(idCard);
			userApprove.setAuthTime(new Date());
			userApprove.setIdCardBack(idCardBackPath);
			userApprove.setIdCardFront(idCardFrontPath);
			userDao.updateUserApprove(userApprove);
			mapPath.put("idCardBack",idCardBackPath);
			mapPath.put("idCardFront",idCardFrontPath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapPath;
	}
	
	@Override
	public Map<String, Object> resultUserMessageMethod(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		Object[] ob = userDao.selectUserMessageByUserId(userId);
		User user = (User) ob[2];
		UserType usertype = (UserType) ob[1];
		UserApprove userApprove = (UserApprove) ob[0];
		map.put("user", user);
		map.put("userType", usertype);
		map.put("userApprove", userApprove);
		return map;
	}
	/**
	 * 注册时验证用户是否存在
	 * @param userPhone
	 * @return
	 */
	@Override
	public User queryUserByuserPhoneMethod(String userPhone) {
		User user = userDao.selectUserByuserPhone(userPhone);
		return user;
	}
	/**
	 * 查找施救者/求救者
	 * @param type
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	@Override
	public Map<String, Object> queryUserlistMethod(int type,Double longitude,Double latitude) {
		List<Object[]> objectlist = userDao.selectUserlist(type,longitude,latitude);
		
		Map<String, Object> maplist = new HashMap<>();
		Object[] ob = new Object[objectlist.size()];
		if(!objectlist.isEmpty()){
			for (int i = 0; i < objectlist.size(); i++) {
				Object[] oblist = objectlist.get(i);
				Map<String, Object> map = new HashMap<>();
				if(type==1){
					ResponseOrder respOrder = (ResponseOrder) oblist[0];
					map.put("responseId", respOrder.getResponseId());
				}else{
					SosOrder sosOrder = (SosOrder) oblist[0];
					map.put("sosId", sosOrder.getSosId());
				}
				UserLocation location = (UserLocation) oblist[2];
				User user = (User) oblist[1];
				map.put("userId", String.valueOf(user.getUserId()));
				map.put("userEmail", user.getUserEmail());
				map.put("userPhone", user.getUserPhone());
				map.put("userPortrait", user.getUserPortrait());
				map.put("userGender", user.getUserGender());
				map.put("userNickName", user.getUserNickName());
				map.put("longitude", location.getLongitude());
				map.put("latitude", location.getLatitude());
				ob[i] = map;
			}
		}
		maplist.put("user",ob);
		return maplist;
	}
	/**
	 * 为了查找附近施救者   为了推送
	 */
	@Override
	public List<User> queryRespUserlistMethod(int type,Double longitude,Double latitude) {
		List<User> list = new ArrayList<>();
		List<Object[]> objectlist = userDao.selectNearUserlist(type, longitude, latitude);
		for (Object[] objects : objectlist) {
			list.add((User)objects[0]);
		}
		return list;
	}
	@Override
	public Map<String, Object> userUpLoadMethod(Integer userId,File userPortrait) {
		Map<String, Object> map = new HashMap<>();
		User user = userDao.selectUserById(userId);
		String userPortraitPath;
		try {
			userPortraitPath = FileServiceImpl.uploadFile(userPortrait);
			user.setUserPortrait(userPortraitPath);
			userDao.updateUser(user);
			map.put("userPortrait", userPortraitPath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	@Override
	public Map<String, Object> queryUserLocationMethod(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		Object[] ob = locationDao.selectUserLocationByUserId(userId);
		UserLocation location = (UserLocation) ob[0];
		User user = (User) ob[1];
		map.put("locatId", location.getLocatId());
		map.put("longitude", location.getLongitude());
		map.put("latitude", location.getLatitude());
		map.put("userId", user.getUserId());
		map.put("userPhone", user.getUserPhone());
		map.put("userPortrait", user.getUserPortrait());
		return map;
	}
	@Override
	public void userLocationUploadMethod(String userPhone, double longitude, double latitude) {
		Object[] ob = locationDao.selectUserLocationByuserPhone(userPhone);
		User user = (User) ob[1];
		UserLocation userLocation = (UserLocation) ob[0];
		if(userLocation!=null){
			userLocation.setLatitude(latitude);
			userLocation.setLongitude(longitude);
			userLocation.setLastUpdateTime(new Date());
			locationDao.updateUserLocation(userLocation);
		}else{
			UserLocation record = new UserLocation();
			record.setLatitude(latitude);
			record.setLongitude(longitude);
			record.setLastUpdateTime(new Date());
			record.setUser(user);
			locationDao.addUserLocation(record);
		}
		
	}
	@Override
	public void modifyUser(User record) {
		userDao.updateUser(record);
	}
	
	@Override
	public Map<String, Object> querySosOrderByuserPhone(String userPhone) {
		Map<String, Object> map = new HashMap<>();
		List<Object[]> oblist = userDao.selectSosOrderByuserPhone(userPhone);
		Object[] sosob = new Object[oblist.size()];
		int i =0;
		for (Object[] ob : oblist) {
			Map<String, Object> sosmap = new HashMap<>();
			SosOrder sosOrder = (SosOrder) ob[0];
			SosStatus sosStatus = (SosStatus) ob[1];
			sosmap.put("sosId", sosOrder.getSosId());
			sosmap.put("createTime", sosOrder.getCreateTime());
			sosmap.put("soskey", sosStatus.getSoskey());
			sosmap.put("sosvalue", sosStatus.getSosvalue());
			sosob[i] = sosmap;
			i++;
		}
		map.put("sosOrder", sosob);
		return map;
	}

}
